package server;

import codec.PacketDecoder;
import codec.PacketEncoder;
import codec.Spliter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import server.requesthandler.AllIdRequestHandler;
import server.requesthandler.CreateGroupRequestHandler;
import server.requesthandler.LoginRequestHandler;
import server.requesthandler.ServerChatMessageHandler;

/**
 * @author czc
 */
public class ServerInitializer extends ChannelInitializer<NioSocketChannel> {
    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
                7,
                4));
        //pipeline.addLast(new Spliter());
        pipeline.addLast(new PacketDecoder());
        pipeline.addLast(new AllIdRequestHandler());
        pipeline.addLast(new CreateGroupRequestHandler());
        pipeline.addLast(new ServerChatMessageHandler());
        pipeline.addLast(new LoginRequestHandler());
        pipeline.addLast(new PacketEncoder());


//        pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
//                ClassResolvers.cacheDisabled(null)));

//        pipeline.addLast(new IdleStateHandler(10,
//                10,
//                20,
//                TimeUnit.MINUTES));
//        pipeline.addLast(new LoggingHandler());
//        pipeline.addLast(new AllIdRequestHandler());
//        pipeline.addLast(new CreateGroupRequestHandler());
//        pipeline.addLast(new ServerChatMessageHandler());
//        pipeline.addLast(new ObjectEncoder());
    }
}
